package com.swt.dao.impl.S_impl;

import com.swt.dao.S_dao.GetCourseDao;
import com.swt.domain.Course;
import com.swt.util.GetCurrentTime;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository("S_getCourseDao")
public class GetCourseDaoImpl implements GetCourseDao {
    /*注入hiberbateTemplate*/
    @Resource(name = "hibernateTemplate")
    private HibernateTemplate hibernateTemplate;

    @Override
    public List<Course> getCourse(int id) {
        /*从数据库取数据*/
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Course.class);
        detachedCriteria.add(Restrictions.eq("courseClubid",id));
        detachedCriteria.add(Restrictions.eq("courseCatagory","团课"));
        //获取当前时间
        String today = GetCurrentTime.getToday();
        /*添加时间条件 只查询今天之后的*/
        detachedCriteria.add(Restrictions.ge("courseTime",today));
        List<Course> list = (List<Course>)hibernateTemplate.findByCriteria(detachedCriteria);

        return list;
    }

    @Override
    public Course getCourseById(int id) {
        Course course = hibernateTemplate.load(Course.class, id);
        return course;
    }

    @Override
    public List<Course> getCoursePersonal(int clubid) {
        /*从数据库取数据*/
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Course.class);
        detachedCriteria.add(Restrictions.eq("courseClubid",clubid));
        detachedCriteria.add(Restrictions.eq("courseCatagory","私教"));
        //获取当前时间
        String today = GetCurrentTime.getToday();
        /*添加时间条件 只查询今天之后的*/
        detachedCriteria.add(Restrictions.ge("courseTime",today));
        List<Course> list = (List<Course>)hibernateTemplate.findByCriteria(detachedCriteria);
        return list;
    }

    @Override
    public List<Course> getcourseBySelect(DetachedCriteria detachedCriteria) {

        /*查询*/
        List<Course> list = (List<Course>)hibernateTemplate.findByCriteria(detachedCriteria);
        return list;
    }
}
